/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-11-22  21:49
 */
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class SimpleCalculator extends JFrame implements ActionListener {
    private JButton addButton, subtractButton, multiplyButton, divideButton;
    private JTextField operand1Field, operand2Field, resultField;

    public static void main(String[] args) {
        new SimpleCalculator();
    }

    public SimpleCalculator() {
        setSize(800, 150);
        setTitle("简易计算器");
        setAlwaysOnTop(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        addButton = createButton("+");
        subtractButton = createButton("-");
        multiplyButton = createButton("*");
        divideButton = createButton("/");

        operand1Field = createTextField();
        operand2Field = createTextField();
        resultField = createTextField();
        resultField.setEditable(false);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        add(new JLabel("操作数1:"), gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        add(operand1Field, gbc);

        gbc.gridx = 3;
        gbc.gridwidth = 1;
        add(new JLabel("操作数2:"), gbc);

        gbc.gridx = 4;
        gbc.gridwidth = 2;
        add(operand2Field, gbc);

        gbc.gridx = 6;
        gbc.gridwidth = 1;
        add(new JLabel("结果:"), gbc);

        gbc.gridx = 7;
        gbc.gridwidth = 2;
        add(resultField, gbc);

        gbc.gridx = 10;
        gbc.gridwidth = 1;
        add(addButton, gbc);

        gbc.gridx = 11;
        add(subtractButton, gbc);

        gbc.gridx = 12;
        add(multiplyButton, gbc);

        gbc.gridx = 13;
        add(divideButton, gbc);

        setVisible(true);
    }

    private JButton createButton(String label) {
        JButton button = new JButton(label);
        button.setPreferredSize(new Dimension(50, 30));
        button.setFont(new Font("Dialog", Font.PLAIN, 14));
        button.addActionListener(this);
        return button;
    }

    private JTextField createTextField() {
        JTextField textField = new JTextField();
        textField.setPreferredSize(new Dimension(70, 30));
        textField.setBorder(new BevelBorder(1));
        return textField;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        String s1 = operand1Field.getText();
        String s2 = operand2Field.getText();

        try {
            BigDecimal x = new BigDecimal(s1);
            BigDecimal y = new BigDecimal(s2);
            BigDecimal num = null;

            if (source == addButton) {
                num = x.add(y);
            } else if (source == subtractButton) {
                num = x.subtract(y);
            } else if (source == multiplyButton) {
                num = x.multiply(y);
            } else if (source == divideButton) {
                if (y.compareTo(BigDecimal.ZERO) != 0) {
                    num = x.divide(y, 2, RoundingMode.HALF_UP);
                } else {
                    JOptionPane.showMessageDialog(this, "除数不能为零", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }

            resultField.setText(String.valueOf(num));
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "请输入有效的数字", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}
